package admin.forms;

import helpers.StringHelper;

import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import admin.models.ApplicationModel;

import entities.Application;
import entities.ApplicationConfig;


public class ApplicationConfigForm {
	public static TreeMap checkValid(HttpServletRequest request, ApplicationConfig appConfig) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String title = (String) request.getParameter("title");
		String description = (String) request.getParameter("description");
		String keyword = (String) request.getParameter("keyword");
		String templateId = (String) request.getParameter("templateId");
		String status = (String) request.getParameter("status");
		String emailAdmin = (String) request.getParameter("emailAdmin");
		String cxGoogleSearch = (String) request.getParameter("cxGoogleSearch");
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		Application app = appModel.get(appConfig.getApplicationId());
		
		if (app == null) {
			check.put("appliationId", "Application Id is not exists");
		}
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not empty.");
			appConfig.setTitle("");
		} else {
			appConfig.setTitle(title);
		}
		
		if (description == null || description.isEmpty()) {
			check.put("description", "Description is not empty.");
			appConfig.setDescription("");
		} else {
			appConfig.setDescription(description);
		}
		
		if (keyword == null || keyword.isEmpty()) {
			check.put("keyword", "Keyword is not empty.");
			appConfig.setKeyword("");
		} else {
			appConfig.setKeyword(keyword);
		}
		
		if (templateId == null || templateId.isEmpty()) {
			check.put("templateId", "Template Id is not empty.");
			appConfig.setTemplateId("");
		} else {
			appConfig.setTemplateId(templateId);
		}
		
		if (status != null && !templateId.isEmpty()) {
			appConfig.setStatus(Integer.parseInt(status));
		}
		
		if (emailAdmin != null && !emailAdmin.isEmpty()) {
			appConfig.setEmailAdmin(emailAdmin);
		}
		
		if (cxGoogleSearch != null && !cxGoogleSearch.isEmpty()) {
			appConfig.setCxGoogleSearch(cxGoogleSearch);
		}
		
		form.put("checkValid", check);
		form.put("appConfig", appConfig);
		
		return form;
	}
}
